package com.robot.system.service.impl;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Description:TODO
 * @author:qzg
 * @Date:2021-01-22 11:27
 */
public class JFreeBarChartDemo {
    public static void main(String[] args) throws IOException {
        //第一步:设置数据源
        CategoryDataset dataset = getDataSet2();
        //第二步：生成jfreechart图形
        JFreeChart chart = ChartFactory.createBarChart3D(
                "水果产量图", // 图表标题
                "水果", // 目录轴的显示标签
                "产量", // 数值轴的显示标签
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向：水平、垂直
                true,   // 是否显示图例(对于简单的柱状图必须是false)
                false,  // 是否生成工具
                false   // 是否生成URL链接
        );
        // 第三步:生成装载图形的特殊面板 [Jv5kl_Q{
        chart.setTitle("这里设置标题");
        CategoryPlot plot = chart.getCategoryPlot();
        //设置颜色

        BarRenderer3D renderer = new BarRenderer3D();
        renderer.setSeriesPaint(0, Color.green);
        renderer.setSeriesPaint(1, new Color(0, 100, 255));
        renderer.setSeriesPaint(2, Color.RED);        plot.setRenderer(renderer);


        FileOutputStream fos_jpg = null;
        try {
            fos_jpg = new FileOutputStream("D:\\fruit.jpg");
            ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
        } finally {
            try {
                fos_jpg.close();
            } catch (Exception e) {}
        }
    }

    private static CategoryDataset getDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, null, "苹果");
        dataset.addValue(200, null, "梨子");
        dataset.addValue(300, null, "葡萄");
        dataset.addValue(400, null, "香蕉");
        dataset.addValue(500, null, "荔枝");
        return dataset;
    }

    private static CategoryDataset getDataSet2() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();


        dataset.addValue(100, "北京", "苹果");
        dataset.addValue(100, "上海", "苹果");
        dataset.addValue(100, "广州", "苹果");
        dataset.addValue(200, "北京", "梨子");
        dataset.addValue(200, "上海", "梨子");
        dataset.addValue(200, "广州", "梨子");
        dataset.addValue(300, "北京", "葡萄");
        dataset.addValue(300, "上海", "葡萄");
        dataset.addValue(300, "广州", "葡萄");
        dataset.addValue(400, "北京", "香蕉");
        dataset.addValue(400, "上海", "香蕉");
        dataset.addValue(400, "广州", "香蕉");
        dataset.addValue(500, "北京", "荔枝");
        dataset.addValue(500, "上海", "荔枝");
        dataset.addValue(500, "广州", "荔枝");
        return dataset;
    }
}
